package com.mfangsoft.zhuangjialong.integration.cardmanage.controller;





import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.facebook.presto.sql.parser.SqlBaseParser.UseContext;
import com.mfangsoft.zhuangjialong.app.entryactivity.service.EntryActivityService;
import com.mfangsoft.zhuangjialong.common.context.UserContext;
import com.mfangsoft.zhuangjialong.common.model.Page;
import com.mfangsoft.zhuangjialong.common.model.ResponseMessage;
import com.mfangsoft.zhuangjialong.common.utils.SysConstant;
import com.mfangsoft.zhuangjialong.common.utils.UserSessionUtil;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfo;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityInfoItem;
import com.mfangsoft.zhuangjialong.integration.activity.model.BaseActivityShop;
import com.mfangsoft.zhuangjialong.integration.activity.service.ActivityService;
import com.mfangsoft.zhuangjialong.integration.cardmanage.model.BaseCardConversion;
import com.mfangsoft.zhuangjialong.integration.cardmanage.model.BaseCardManage;
import com.mfangsoft.zhuangjialong.integration.cardmanage.model.BaseCardShop;
import com.mfangsoft.zhuangjialong.integration.cardmanage.service.CardManageService;
import com.mfangsoft.zhuangjialong.integration.commentactivity.model.BaseCommentActivity;
import com.mfangsoft.zhuangjialong.integration.commentactivity.service.CommentActivityService;
import com.mfangsoft.zhuangjialong.integration.order.model.BaseOrder;
import com.mfangsoft.zhuangjialong.integration.permission.model.PermissionEntity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.model.BaseStatisticsActivity;
import com.mfangsoft.zhuangjialong.integration.statisticsactivity.service.StatisticsActivityService;
import com.mfangsoft.zhuangjialong.integration.user.model.UserEntity;
import com.mfangsoft.zhuangjialong.integration.user.service.UserService;



@Controller
/*@RequestMapping("/abc")*/
@RequestMapping("/server")
public class CardManageController {
	

	@Autowired
	private CardManageService cardManageService; 
	
	/**
	 * 新增卡券
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/addCardManage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public ResponseMessage<String> addCardManage(@RequestBody BaseCardManage baseCardManage){
		ResponseMessage<String> message = new ResponseMessage<String>();
		//BaseCardManage baseCardManage = new BaseCardManage();
		//UseContext
		//baseActivityInfo.setCreater((String) session.getAttribute("user"));
		/*System.out.println(UserContext.getCurrentUser());
		System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
		
		
		//创建人

		try {
			
			baseCardManage.setCreate(UserContext.getCurrentUser().getTag());
			
			cardManageService.addCardManage(baseCardManage);
			
			BaseCardShop baseCardShop = new BaseCardShop();    //需要修改多条插入
		    
			baseCardShop.setCard_id(baseCardManage.getId());//卡券id
			
			cardManageService.addCardShop(baseCardShop);//店铺和卡券中间表
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	
	}
	
	/**
	 * 单条数据查询卡券
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/selectCardManageOne/{id}",method=RequestMethod.POST)
	/*@ResponseBody
	@CrossOrigin*/
	public ResponseMessage<BaseCardManage> selectCardManageOne(@PathVariable int id){
		ResponseMessage<BaseCardManage> message = new ResponseMessage<BaseCardManage>();
		try {
			message.setResult(cardManageService.selectCardManageOne(id));
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	
	}
	/**
	 * 卡券删除
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/deleteCardManage/{id}",method=RequestMethod.POST)
	/*@ResponseBody
	@CrossOrigin*/
	public ResponseMessage<BaseCardManage> deleteCardManage(@PathVariable int id){
		ResponseMessage<BaseCardManage> message = new ResponseMessage<BaseCardManage>();
		try {
			cardManageService.deleteCardManage(id);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
		
	}
	
	/**
	 * 卡券编辑
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/updateCardManageOne",method=RequestMethod.POST)
	/*@ResponseBody
	@CrossOrigin*/
	public ResponseMessage<BaseCardManage> updateCardManageOne(@RequestBody BaseCardManage baseCardManage){
		ResponseMessage<BaseCardManage> message = new ResponseMessage<BaseCardManage>();
		try {
			cardManageService.updateCardManageOne(baseCardManage);
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
	
	}
	/**
	 * 卡券列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectCardManagePage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Page<BaseCardManage> selectCardManagePage(@RequestBody Page<BaseCardManage> page) throws ParseException{
		
		return cardManageService.selectBaseCardManage(page);
	}
	
	/**
	 * 领取卡券
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/addCardConversion",method=RequestMethod.POST)
	/*@ResponseBody
	@CrossOrigin*/
	public ResponseMessage<String> addCardConversion(@RequestBody BaseCardConversion baseCardConversion){
		ResponseMessage<String> message = new ResponseMessage<String>();
		//BaseCardManage baseCardManage = new BaseCardManage();
		//UseContext
		//baseActivityInfo.setCreater((String) session.getAttribute("user"));
		/*System.out.println(UserContext.getCurrentUser());
		System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
		String date = new SimpleDateFormat("yyyyMMddHHmmssSSS").format(new Date());//获取时间
		
	    String aCode = "KQ"+date + String.valueOf(Math.round(Math.random()*1000)); //生成活动号
		
		//创建人

		try {
			//领取卡券功能是创建兑换卡券，创建兑换码。兑换时修改此条数据
			baseCardConversion.setCode(aCode);
			
			cardManageService.addCardConversion(baseCardConversion);
			
			BaseCardConversion cc = new BaseCardConversion();
			cc.setCard_id(baseCardConversion.getCard_id());
			int card_num = cardManageService.selectCardConversion(cc);//通过卡ID  查询 卡券创建的数量。获得领取数量。传给卡券表。
			BaseCardManage baseCardManage = new BaseCardManage();
			baseCardManage.setId(baseCardConversion.getCard_id());
			baseCardManage.setGet(card_num);
			cardManageService.updateCardManageOne(baseCardManage);
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
		
		
	}
	/**
	 * 兑换卡券
	 * @param 
	 * @return
	 */
	
	@RequestMapping(value="/updateCardConversion",method=RequestMethod.POST)
	/*@ResponseBody
	@CrossOrigin*/
	public ResponseMessage<String> updateCardConversion(@RequestBody BaseCardConversion baseCardConversion){
		ResponseMessage<String> message = new ResponseMessage<String>();
		//BaseCardManage baseCardManage = new BaseCardManage();
		//UseContext
		//baseActivityInfo.setCreater((String) session.getAttribute("user"));
		/*System.out.println(UserContext.getCurrentUser());
		System.out.println(UserContext.getCurrentUser().getUserInfoEntity().getName());*/
		
		
		//创建人

		try {
			
			
			cardManageService.updateCardConversion(baseCardConversion);
			
			message.setCode(SysConstant.SUCCESS_CODE);
			message.setMessage(SysConstant.SUCCESS_MSG);
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			message.setCode(SysConstant.FAILURE_CODE);
			message.setMessage(SysConstant.FAILURE_MSG);
		}
		return message;
		
		
	}
	/**
	 * 兑换列表
	 * @param baseActivityInfo
	 * @return
	 * @throws ParseException 
	 */
	
	@RequestMapping(value="/selectCardConversionPage",method=RequestMethod.POST)
	@ResponseBody
	@CrossOrigin
	public Page<BaseCardConversion> selectCardConversionPage(@RequestBody Page<BaseCardConversion> page) throws ParseException{
		
		return cardManageService.selectCardConversionPage(page);
	}
	
	
}
